package me.zhengjie.modules.system.service.mapstruct;

import me.zhengjie.base.BaseMapper;
import me.zhengjie.modules.system.domain.Dept;
import me.zhengjie.modules.system.domain.Org;
import me.zhengjie.modules.system.repository.DeptRepository;
import me.zhengjie.modules.system.repository.OrgRepository;
import me.zhengjie.modules.system.service.dto.DeptDto;
import me.zhengjie.modules.system.service.dto.OrgDto;
import me.zhengjie.utils.SpringContextHolder;
import org.mapstruct.AfterMapping;
import org.mapstruct.Mapper;
import org.mapstruct.MappingTarget;
import org.mapstruct.ReportingPolicy;

/**
 * @Author: xin.peng
 * @Date: 2020/12/2
 * @Description :
 */
@Mapper(componentModel = "spring",unmappedTargetPolicy = ReportingPolicy.IGNORE)
public interface OrgMapper  extends BaseMapper<OrgDto, Org> {

    /**
     * 转换后的特殊处理
     * @param orgDto /
     * @return /
     */
    @AfterMapping
    default OrgDto dealDto(@MappingTarget OrgDto orgDto) {
        OrgRepository orgRepository = SpringContextHolder.getBean(OrgRepository.class);
        if(orgRepository.countByParentId(orgDto.getId()) > 0){
            orgDto.setHasChildren(true);
            orgDto.setLeaf(false);
        }
        return orgDto;
    }
}
